package com.twang.stock.service;

import com.alibaba.fastjson.JSONObject;
import com.twang.stock.entity.DfcfBanKuaiMaoLiLv;
import com.twang.stock.entity.ThsGeGuReZDian;
import com.twang.stock.entity.WenCaiData;
import com.twang.stock.mapper.CompanyStockMinutesMapper;
import com.twang.stock.mapper.LongHuInfoMapper;
import com.twang.stock.mapper.LongHuInfoXiWeiMapper;
import com.twang.stock.mapper.WenCaiDataMapper;
import com.twang.stock.service.jinyan.HolidayService;
import com.twang.stock.task.NotifyStockBuyTask;
import com.twang.stock.task.QueryBanKuaiAndDaPan;
import com.twang.stock.utils.date.HolidayCopy;
import com.twang.stock.redis.RedisUtil;
import org.assertj.core.util.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class WenCaiDataService {
    @Resource
    private LongHuInfoMapper longHuInfoMapper;

    @Resource
    private CompanyStockMinutesMapper companyStockMinutesMapper;

    @Resource
    private IndexService indexService;

    @Resource
    private ZhangTingBanService zhangTingBanService;

    @Resource
    private HolidayService holidayService;

    @Resource
    private LongHuInfoXiWeiMapper longHuInfoXiWeiMapper;

    @Resource
    private ZhangTingBanTaskService zhangTingBanTaskService;

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private WenCaiDataMapper wenCaiDataMapper;


    @Autowired
    private NotifyStockBuyTask notifyStockBuyTask;

    public List<DfcfBanKuaiMaoLiLv>  获取年度涨幅前20(){
        List<DfcfBanKuaiMaoLiLv> maoLiLvs;
        String key = "WENCAIDATASERVICE_REDIS_" + HolidayCopy.getRealToday();
        Object obj = redisUtil.get(key);
        if (null == obj||obj.toString().length()<50) {
            List<WenCaiData> wenCaiDataList = wenCaiDataMapper.selectAll();
            maoLiLvs= getMaoLiLvDTO(wenCaiDataList);
            zhangTingBanTaskService.initKanDian(maoLiLvs, Boolean.FALSE);
            //30 Min 成交额
            zhangTingBanTaskService.getTrade30MinutesVol(maoLiLvs);
            redisUtil.set(key, JSONObject.toJSONString(maoLiLvs));
        } else {
            maoLiLvs = JSONObject.parseArray(obj.toString(), DfcfBanKuaiMaoLiLv.class);
        }
        return maoLiLvs;
    }

    /**
     * 获取看点
     * @param wenCaiDataList
     */
    private List<DfcfBanKuaiMaoLiLv> getMaoLiLvDTO( List<WenCaiData> wenCaiDataList) {
        List<DfcfBanKuaiMaoLiLv> maoLiLvs = Lists.newArrayList();
        for (int i = 0; i < wenCaiDataList.size(); i++) {
            WenCaiData wenCaiData = wenCaiDataList.get(i);
            notifyStockBuyTask.setZhangDie(wenCaiData);
            /** * 6-今日大盘 */
            try {
                String code = wenCaiData.getCode();
                String stockCode = wenCaiData.getStockCode();
                DfcfBanKuaiMaoLiLv dfcfBanKuaiMaoLiLv = new DfcfBanKuaiMaoLiLv();
                dfcfBanKuaiMaoLiLv.setCode(code);
                dfcfBanKuaiMaoLiLv.setName(wenCaiData.getStockName());
                dfcfBanKuaiMaoLiLv.setMll(new BigDecimal(wenCaiData.getLatestChange()));
                dfcfBanKuaiMaoLiLv.setZrZhangDie(companyStockMinutesMapper.queryYesterZhangDie(wenCaiData.getStockCode(), wenCaiData.getDate()));
                dfcfBanKuaiMaoLiLv.setLianXuZhang(indexService.isLianXuShangZhang(dfcfBanKuaiMaoLiLv));
                dfcfBanKuaiMaoLiLv.setCje(wenCaiData.getChangeRate());
                List<ThsGeGuReZDian> thsGeGuReZDianList = QueryBanKuaiAndDaPan.查询个股热点(code);
                //查询涨停原因
                ThsGeGuReZDian thsGeGuReZDian = new ThsGeGuReZDian();
                thsGeGuReZDian.setTagName("历史涨停原因");
                String ztDesc = zhangTingBanService.zhangtingReason(stockCode);
                if (StringUtils.isNotBlank(ztDesc)) {
                    thsGeGuReZDian.setZtDesc(ztDesc);
                }
                //
                thsGeGuReZDianList.add(thsGeGuReZDian);
               dfcfBanKuaiMaoLiLv.setKanDian(thsGeGuReZDianList);
                maoLiLvs.add(dfcfBanKuaiMaoLiLv);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return maoLiLvs;
    }
}
